//

// (  )
//

package com.tools.acceleratecore.xlib.tool.impl;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import com.tools.acceleratecore.xlib.XLibFactory;
import com.tools.acceleratecore.xlib.tool.intf.ISensorObserver;
import com.tools.acceleratecore.xlib.tool.intf.ISensorObserverListener;
import com.tools.acceleratecore.xlib.xlib.impl.XObserver;
import java.util.Iterator;

public class SensorObserver extends XObserver<ISensorObserverListener> implements ISensorObserver {
    private Context mContext = null;
    private boolean mInited = false;
    private SensorManager mSensorManager = null;
    private Sensor mSensor = null;
    private SensorEventListener mSensorEventListener = null;
    private int mRate = 3;

    public SensorObserver() {
        this.mContext = XLibFactory.getApplication();
        this._init();
    }

    private void _init() {
        this.mSensorManager = (SensorManager)this.mContext.getSystemService("sensor");
        this.mSensorEventListener = new SensorEventListener() {
            public void onAccuracyChanged(Sensor arg0, int arg1) {
                synchronized(SensorObserver.this.mListListener) {
                    Iterator var5 = SensorObserver.this.getListListener().iterator();

                    while(var5.hasNext()) {
                        ISensorObserverListener listener = (ISensorObserverListener)var5.next();
                        listener.onAccuracyChanged(arg0, arg1);
                    }

                }
            }

            public void onSensorChanged(SensorEvent arg0) {
                synchronized(SensorObserver.this.mListListener) {
                    Iterator var4 = SensorObserver.this.getListListener().iterator();

                    while(var4.hasNext()) {
                        ISensorObserverListener listener = (ISensorObserverListener)var4.next();
                        listener.onSensorChanged(arg0);
                    }

                }
            }
        };
    }

    public boolean setSensorType(int nType, int nRate) {
        this.mSensor = this.mSensorManager.getDefaultSensor(nType);
        this.mRate = nRate;
        return this.mSensor != null;
    }

    public boolean startListen() {
        if (this.mInited) {
            return false;
        } else {
            this.mInited = true;
            return this.mSensorManager.registerListener(this.mSensorEventListener, this.mSensor, this.mRate);
        }
    }

    public void stopListen() {
        if (this.mInited) {
            this.mInited = false;
            this.mSensorManager.unregisterListener(this.mSensorEventListener);
        }
    }
}
